from PyQt5 import QtCore, QtGui, QtWidgets
from tools.generate_psd import generate_strong_password

class PasswordGeneratorDialog(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(PasswordGeneratorDialog, self).__init__(parent)
        self.setWindowTitle("Generate Password")
        self.setGeometry(400, 200, 300, 250)

        self.length_label = QtWidgets.QLabel("Length:", self)
        self.length_label.setGeometry(20, 20, 60, 30)
        self.length_input = QtWidgets.QSpinBox(self)
        self.length_input.setGeometry(80, 20, 60, 30)
        self.length_input.setRange(4, 32)
        self.length_input.setValue(12)

        self.uppercase_checkbox = QtWidgets.QCheckBox("Include Uppercase", self)
        self.uppercase_checkbox.setGeometry(20, 60, 150, 30)
        self.uppercase_checkbox.setChecked(True)

        self.lowercase_checkbox = QtWidgets.QCheckBox("Include Lowercase", self)
        self.lowercase_checkbox.setGeometry(20, 90, 150, 30)
        self.lowercase_checkbox.setChecked(True)

        self.digits_checkbox = QtWidgets.QCheckBox("Include Digits", self)
        self.digits_checkbox.setGeometry(20, 120, 150, 30)
        self.digits_checkbox.setChecked(True)

        self.symbols_checkbox = QtWidgets.QCheckBox("Include Symbols", self)
        self.symbols_checkbox.setGeometry(20, 150, 150, 30)
        self.symbols_checkbox.setChecked(True)

        # self.generate_button = QtWidgets.QPushButton("Generate", self)
        # self.generate_button.setGeometry(100, 200, 80, 30)
        # self.generate_button.clicked.connect(self.generate_password)
        self.generate_button = QtWidgets.QPushButton("Generate", self)
        self.generate_button.setGeometry(100, 200, 80, 30)
        self.generate_button.clicked.connect(self.show_password_dialog)

    def generate_password(self):
        length = self.length_input.value()
        include_uppercase = self.uppercase_checkbox.isChecked()
        include_lowercase = self.lowercase_checkbox.isChecked()
        include_digits = self.digits_checkbox.isChecked()
        include_symbols = self.symbols_checkbox.isChecked()
        password = generate_strong_password(length, include_uppercase, include_lowercase, include_digits, include_symbols)
        
        QtWidgets.QMessageBox.information(self, "Generated Password", f"Your generated password is: {password}")

        self.parent().password_input.setText(password)
        self.accept()
        return password
    def show_password_dialog(self):
        password = self.generate_password()

        # 展示密码
        password_dialog = PasswordDisplayDialog(password, parent=self)
        password_dialog.exec_()

class PasswordDisplayDialog(QtWidgets.QDialog):
    def __init__(self, password, parent=None):
        super(PasswordDisplayDialog, self).__init__(parent)
        self.setWindowTitle("Generated Password")
        self.setGeometry(500, 300, 300, 150)

        # 展会生成的密码
        self.password_label = QtWidgets.QLabel(password, self)
        self.password_label.setGeometry(20, 20, 260, 30)
        self.password_label.setAlignment(QtCore.Qt.AlignCenter)

        # 复制密码
        self.copy_button = QtWidgets.QPushButton("Copy Password", self)
        self.copy_button.setGeometry(80, 80, 140, 30)
        self.copy_button.clicked.connect(self.copy_password)

    def copy_password(self):
        clipboard = QtWidgets.QApplication.clipboard()
        clipboard.setText(self.password_label.text())
        QtWidgets.QMessageBox.information(self, "Copied", "Password copied to clipboard!")

class RegisterDialog(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(RegisterDialog, self).__init__(parent)
        self.setWindowTitle("Register")
        self.setGeometry(400, 200, 300, 300)

        self.username_label = QtWidgets.QLabel("Username:", self)
        self.username_label.setGeometry(20, 30, 80, 30)
        self.username_input = QtWidgets.QLineEdit(self)
        self.username_input.setGeometry(100, 30, 170, 30)

        self.password_label = QtWidgets.QLabel("Password:", self)
        self.password_label.setGeometry(20, 80, 80, 30)
        self.password_input = QtWidgets.QLineEdit(self)
        self.password_input.setGeometry(100, 80, 170, 30)
        self.password_input.setEchoMode(QtWidgets.QLineEdit.Password)

        self.confirm_password_label = QtWidgets.QLabel("Confirm Password:", self)
        self.confirm_password_label.setGeometry(20, 130, 120, 30)
        self.confirm_password_input = QtWidgets.QLineEdit(self)
        self.confirm_password_input.setGeometry(140, 130, 130, 30)
        self.confirm_password_input.setEchoMode(QtWidgets.QLineEdit.Password)

        self.register_button = QtWidgets.QPushButton("Register", self)
        self.register_button.setGeometry(40, 180, 80, 30)
        self.register_button.clicked.connect(self.handle_register)

        self.generate_button = QtWidgets.QPushButton("Generate Password", self)
        self.generate_button.setGeometry(150, 180, 120, 30)
        self.generate_button.clicked.connect(self.show_password_generator)

    def handle_register(self):
        username = self.username_input.text()
        password = self.password_input.text()
        confirm_password = self.confirm_password_input.text()

        if password != confirm_password:
            QtWidgets.QMessageBox.warning(self, "Error", "Passwords do not match")
        else:
            print(f"Registered Username: {username}, Password: {password}")
            self.accept()

    def show_password_generator(self):
        dialog = PasswordGeneratorDialog(self)
        dialog.exec_()

class LoginDialog(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(LoginDialog, self).__init__(parent)
        self.setWindowTitle("Login")
        self.setGeometry(400, 200, 300, 200)

        self.username_label = QtWidgets.QLabel("Username:", self)
        self.username_label.setGeometry(20, 30, 80, 30)
        self.username_input = QtWidgets.QLineEdit(self)
        self.username_input.setGeometry(100, 30, 170, 30)

        self.password_label = QtWidgets.QLabel("Password:", self)
        self.password_label.setGeometry(20, 80, 80, 30)
        self.password_input = QtWidgets.QLineEdit(self)
        self.password_input.setGeometry(100, 80, 170, 30)
        self.password_input.setEchoMode(QtWidgets.QLineEdit.Password)

        self.login_button = QtWidgets.QPushButton("Login", self)
        self.login_button.setGeometry(100, 130, 80, 30)
        self.login_button.clicked.connect(self.handle_login)

    def handle_login(self):
        username = self.username_input.text()
        password = self.password_input.text()
        print(f"Username: {username}, Password: {password}")
        self.accept()

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        MainWindow.setMouseTracking(True)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(-30, 10, 801, 481))
        self.label.setTextFormat(QtCore.Qt.AutoText)
        self.label.setIndent(100)
        self.label.setObjectName("label")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(150, 510, 161, 51))
        self.pushButton.setStyleSheet("background-color: rgb(255, 170, 0);")
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(490, 510, 161, 51))
        self.pushButton_2.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
        self.pushButton_2.setMouseTracking(True)
        self.pushButton_2.setStyleSheet("background-color: rgb(255, 170, 0);")
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_2.clicked.connect(self.show_login_dialog)

        self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_3.setGeometry(QtCore.QRect(320, 510, 161, 51))
        self.pushButton_3.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
        self.pushButton_3.setMouseTracking(True)
        self.pushButton_3.setStyleSheet("background-color: rgb(255, 170, 0);")
        self.pushButton_3.setObjectName("pushButton_3")
        self.pushButton_3.clicked.connect(self.show_register_dialog)

        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(580, 360, 221, 51))
        self.label_2.setObjectName("label_2")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "<html><head/><body><p align=\"center\"><span style=\" font-size:36pt; font-weight:600;\">The Password Manage Tools</span></p><p align=\"center\"><span style=\" font-size:36pt; font-weight:600;\">Welcome</span></p></body></html>"))
        self.pushButton.setText(_translate("MainWindow", "人脸识别登录"))
        self.pushButton_2.setText(_translate("MainWindow", "账号密码登录"))
        self.pushButton_3.setText(_translate("MainWindow", "注册账号"))
        self.label_2.setText(_translate("MainWindow", "<html><head/><body><p align=\"center\"><span style=\" font-size:12pt;\">By Matilda韩泽斌</span></p></body></html>"))

    def show_login_dialog(self):
        dialog = LoginDialog()
        if dialog.exec_() == QtWidgets.QDialog.Accepted:
            print("Login successful")

    def show_register_dialog(self):
        dialog = RegisterDialog()
        if dialog.exec_() == QtWidgets.QDialog.Accepted:
            print("Registration successful")




if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())